import { BaseApiService } from '../base/api.service';
import type { Engineer, EngineerStats, TopCommitter } from '@/types/business/engineer';
import type { PaginatedResponse } from '@/types/common/api';
import type { EngineerEmail } from '@/types/business/engineer';
import type { AuthorInfo } from '@/types/business/engineer';

/**
 * 工程师服务类
 */
export class EngineerService extends BaseApiService {
  constructor() {
    super('/api/engineer');
  }

  /**
   * 搜索工程师
   */
  async searchEngineers(
    name: string,
    page: number = 1,
    size: number = 20,
  ): Promise<PaginatedResponse<Engineer>> {
    const params = { name, page, size };
    return this.getPaginated<Engineer>('search', params);
  }

  /**
   * 获取部门下的工程师列表
   */
  async getEngineersByDept(deptId: string): Promise<Engineer[]> {
    return this.get<Engineer[]>(`engineers/all?deptId=${deptId}`);
  }

  /**
   * 获取工程师统计信息
   */
  async getEngineerStats(): Promise<EngineerStats> {
    return this.get<EngineerStats>('stats');
  }

  /**
   * 获取历史顶级提交者
   */
  async getHistoricalTopCommitters(
    limitStr: string = '5',
    sortBy: string = 'total_changes',
    sortDirection: string = 'desc',
  ): Promise<TopCommitter[]> {
    // 直接使用httpService调用，因为这个API端点不在当前baseUrl下
    const { httpService } = await import('../base/http.service');
    const params = { limitStr: limitStr, sortBy: sortBy, sortDirection: sortDirection };
    const response = await httpService.query<TopCommitter[]>(
      '/api/gitlog/stats/top-historical',
      params,
    );
    return response.data || (response as unknown as TopCommitter[]);
  }

  /**
   * 获取最近一个月顶级提交者
   */
  async getLastMonthTopCommitters(
    limitStr: string = '5',
    sortBy: string = 'total_changes',
    sortDirection: string = 'desc',
  ): Promise<TopCommitter[]> {
    // 直接使用httpService调用，因为这个API端点不在当前baseUrl下
    const { httpService } = await import('../base/http.service');
    const params = { limitStr: limitStr, sortBy: sortBy, sortDirection: sortDirection };
    const response = await httpService.query<TopCommitter[]>(
      '/api/gitlog/stats/top-last-month',
      params,
    );
    return response.data || (response as unknown as TopCommitter[]);
  }

  async getEmailsByEngineerId(engineerId: string): Promise<EngineerEmail[]> {
    return this.get<EngineerEmail[]>(`emails?engineerId=${engineerId}`);
  }

  async removeEmail(id: string): Promise<boolean> {
    const { httpService } = await import('../base/http.service');
    const response = await httpService.delete<boolean>(`/api/engineer/emails/${id}`);
    return response.data;
  }

  async addEmail(
    authorFullname: string,
    authorEmail: string,
    engineerId: string,
  ): Promise<EngineerEmail> {
    return this.post<EngineerEmail>('emails', { authorFullname, authorEmail, engineerId });
  }

  public async getDistinctAuthors(): Promise<AuthorInfo[]> {
    return this.get<AuthorInfo[]>('distinct-authors');
  }
}

// 导出单例实例
export const engineerService = new EngineerService();
